package org.fusionide.builder.parsers.cfml.tags;

import java.util.Vector;

import org.fusionide.builder.parsers.IParserHandler;
import org.fusionide.builder.parsers.ParserException;
import org.fusionide.builder.parsers.cfml.CFMLTag;
import org.fusionide.builder.parsers.cfml.Variable;

public class Query extends CFMLTag {
	private String blockfactor;

	private String cachedafter;

	private String cachedwithin;

	private String datasource;

	private String dbtype;

	private String debug;

	private String maxrows;

	private String name;

	private String password;

	private String result;

	private String timeout;

	private String username;

	public Query(String attributes, int startOffset) {
		super(attributes, startOffset);
		if (attributes == null)
			return;

		this.blockfactor = getAttribute("blockfactor");
		this.cachedafter = getAttribute("cachedafter");
		this.cachedwithin = getAttribute("cachedwithin");
		this.datasource = getAttribute("datasource");
		this.dbtype = getAttribute("dbtype");
		this.debug = getAttribute("debug");
		this.maxrows = getAttribute("maxrows");
		this.name = getAttribute("name");
		this.password = getAttribute("password");
		this.result = getAttribute("result");
		this.timeout = getAttribute("timeout");
		this.username = getAttribute("username");
	}

	@Override
	public boolean requiresClosingTag() {
		return true;
	}

	@Override
	public boolean acceptsChildren(CFMLTag tag) {
		if (super.acceptsChildren(tag)) {
			if (tag instanceof Query)
				return false;
			return true;
		}
		return false;
	}

	@Override
	public boolean validate(IParserHandler parserHandler) {
		if (super.validate(parserHandler)) {

			if (name == null || name.equalsIgnoreCase("")) {
				if (parserHandler != null)
					parserHandler.addMarker(new ParserException("<cfquery> Requires name attribute", ParserException.SEVERITY_ERROR, startLine));
				return false;
			}
			if ((datasource == null || datasource.equalsIgnoreCase(""))) {
				if (dbtype == null || !dbtype.equalsIgnoreCase("query")) {
					if (parserHandler != null)
						parserHandler.addMarker(new ParserException("<cfquery> Requires datasource attribute", ParserException.SEVERITY_ERROR, startLine));
					return false;
				}
			}

			return true;
		} else
			return false;
	}

	@Override
	public Vector<Variable> getVariables(int offset) {
		Vector<Variable> vars = new Vector<Variable>();
		if (offset < endOffset)
			return vars;

		vars.add(new Variable(name, startLine, false, Variable.query));
		
		vars.addAll(super.getChildVariables(offset));

		return vars;
	}

	public String getBlockfactor() {
		return blockfactor;
	}

	public void setBlockfactor(String blockfactor) {
		this.blockfactor = blockfactor;
	}

	public String getCachedafter() {
		return cachedafter;
	}

	public void setCachedafter(String cachedafter) {
		this.cachedafter = cachedafter;
	}

	public String getCachedwithin() {
		return cachedwithin;
	}

	public void setCachedwithin(String cachedwithin) {
		this.cachedwithin = cachedwithin;
	}

	public String getDatasource() {
		return datasource;
	}

	public void setDatasource(String datasource) {
		this.datasource = datasource;
	}

	public String getDbtype() {
		return dbtype;
	}

	public void setDbtype(String dbtype) {
		this.dbtype = dbtype;
	}

	public String getDebug() {
		return debug;
	}

	public void setDebug(String debug) {
		this.debug = debug;
	}

	public String getMaxrows() {
		return maxrows;
	}

	public void setMaxrows(String maxrows) {
		this.maxrows = maxrows;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getTimeout() {
		return timeout;
	}

	public void setTimeout(String timeout) {
		this.timeout = timeout;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
